
function regexId (url) {
  const regex = /{([^}]+)}/;
  const [_, key] = url.match(regex) || [];
  if (!key) {
    console.log("未找到匹配的ID");
  }
  return key;
}

import axios from 'axios';
import executeGif from '@/static/images/execute.gif'
import warning from '@/static/images/warning.png'

export default {
  data () {
    return {
      result: {},
      visible: false,
      loading: false,
      taskId: null,
      executeGif,
      warning
    };
  },
  computed: {
    url () {
      return this.value?.url
    },
    taskUrl () {
      return `/tenant/task/{id}`
    },
    // idCountExceeded () {
    //   return this.value?.selection?.length || 0
    // },
    // idLimitReached () {
    //   return fulfill => fulfill?.count?.length || 0
    // },
    // name () {
    //   return this?.data?.name
    // },
    content () {
      return `确定${this.name}吗?`
    },
    progressContent () {
      const { totalNum, finishedNum, unfinishedNum } = this.result
      return `${this.name}共${totalNum || this.idCountExceeded}单，已处理${finishedNum || 0}单，剩余${unfinishedNum || this.idCountExceeded}单`
    },
    tipContent () {
      return `正在进行${this.name}操作，离开页面后，批量操作结果可点击导航栏-任务中心图标查看。`
    }
  },
  methods: {
    async batchInterface () {
      try {
        let response = await axios.post(this.url, this.data)
        if (response?.code === 0) {
          // this.taskId = response?.data;
          if (!response?.data) {
            this.visible = false
            this.loading = false
            this.$message({ type: "success", message: response?.message });
            this.$table.search();
          }
          return response?.data;
        } else {
          this.loading = false;
          this.$message({ type: "error", message: response?.message });
        }
      } catch (error) {
        this.loading = false;
      }
    },
    async loopUntilProgressIsOne (taskUrl, id) {
      let result;
      const interval = 3000; // 调用接口的时间间隔，单位为毫秒
      do {
        try {
          result = await this.fetchBatchId(taskUrl, id);
          await new Promise(resolve => setTimeout(resolve, interval)); // 等待一段时间后再继续调用接口
          this.$set(this, 'result', result);
        } catch (error) {
          console.error('批量任务进程异常' + error);
          break;
        }
      } while (result.progress !== 1 && !this.stopLoop);

      return result;
    },
    async fetchBatchId (taskUrl, id) {
      const key = regexId(taskUrl);
      const apiUrl = taskUrl.replace(`{${key}}`, id);
      try {
        const response = await axios.get(apiUrl);
        return response.data;
      } catch (error) {
        console.error(error);
        throw error;
      }
    },
    async procedure (taskUrl, id) {
      // this.loading = true
      const response = await this.loopUntilProgressIsOne(taskUrl, id)
      this.result = response
      if (this.result.progress === 1) {
        this.visible = false;
        this.$message({ type: "success", message: "操作成功" });
        this.$emit("refresh");
        this.$table.search();
      }
    }
  }
};
